package cn.com.headfree.mouldshow.controller;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.entity.TMouldInfo;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.mouldshow.service.HumanDataImportService;
import cn.com.headfree.mouldshow.service.HumanDataTempService;
import cn.com.headfree.mouldshow.service.TMouldInfoService;
import cn.com.headfree.vo.ImportDataVo;
import com.gta.edu.sdk.common.dto.LoginUserInfo;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;

/**
 *数据导入模板
 */
@Controller
@RequestMapping(value = "/humanDataImport", produces = "application/json;charset=UTF-8")
public class HumanDataImportController extends ExBaseController {
	private static final Logger LOG = LogManager.getLogger(HumanDataImportController.class);

	@Autowired
	private HumanDataImportService humanDataImportService;

	@Autowired
	private HumanDataTempService humanDataTempService;


	/**
	 * 数据批量导入临时库
	 */
	@PostMapping("/importHumanTempDatas")
	@ResponseBody
	public ResultData importHumanTempDatas(HttpServletRequest request,@RequestParam String easyName,@RequestParam("file") MultipartFile file){
		ResultData result = new ResultData();
		LOG.info("===========================开始导入===============================");
		User user = this.getLoginUserInfo(request);
		if(user == null || StringUtils.isBlank(user.getBaseOrgId())){
			result.setErrorMsg("未获取到当前登录人员信息，不允许导入");
			result.setSuccess(false);
			return result;
		}
		try {
			String str = humanDataImportService.importHumanDatas(easyName,file,user);
			if(StringUtils.isBlank(str)){
				result.setSuccess(true);
				result.setData("导入成功");
			}else{
				result.setSuccess(false);
				result.setErrorMsg(str);
			}
		}catch (Exception e){
			result.setSuccess(false);
			result.setErrorMsg("导入失败");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 数据批量导入正式库
	 */
	@PostMapping("/importHumanDatas")
	@ResponseBody
	public ResultData importHumanDatas(@RequestBody ImportDataVo importDataVo,HttpServletRequest request){
		ResultData result = new ResultData();
		if(StringUtils.isBlank(importDataVo.getEasyName())){
			result.setSuccess(false);
			result.setData("参数错误");
			return result;
		}
		User user = this.getLoginUserInfo(request);
		try {
			String message = "";
			message = humanDataTempService.importHumanDatas(importDataVo.getEasyName(),importDataVo.getIdList(),user);
			result.setSuccess(true);
			if(StringUtils.isNotBlank(message)){
				result.setErrorMsg(message);
				result.setSuccess(false);
			}else{
				result.setData("导入成功");
			}
		}catch (Exception e){
			result.setSuccess(false);
			result.setData("导入失败");
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/mouldDownload")
	public String downloadFile(HttpServletRequest request, HttpServletResponse response,@RequestParam String easyName)	throws UnsupportedEncodingException {
		String filePath = "";
		// 下载文件能正常显示中文
		String fileName = "";
		switch (easyName){
			case "jbxx":
				filePath = "/templates/人资管理基本信息导入模板.xlsx";
				fileName= "人资管理基本信息导入模板.xlsx";
				break;
			case "xlxx":
				filePath = "/templates/人资管理学历信息导入模板.xlsx";
				fileName= "人资管理学历信息导入模板.xlsx";
				break;
			case "gzjl":
				filePath = "/templates/人资管理工作经历导入模板.xlsx";
				fileName= "人资管理工作经历导入模板.xlsx";
				break;
			case "pxsk":
				filePath = "/templates/人资管理培训及授课信息导入模板.xlsx";
				fileName= "人资管理培训及授课信息导入模板.xlsx";
				break;
			case "grry":
				filePath = "/templates/人资管理个人荣誉导入模板.xlsx";
				fileName= "人资管理个人荣誉导入模板.xlsx";
				break;
			case "jsks":
				filePath = "/templates/人资管理竞赛考试信息导入模板.xlsx";
				fileName= "人资管理竞赛考试信息导入模板.xlsx";
				break;
			case "zjrc":
				filePath = "/templates/人资管理专家人才称号导入模板.xlsx";
				fileName= "人资管理专家人才称号导入模板.xlsx";
				break;
			case "zyzg":
				filePath = "/templates/人资管理专业技术资格导入模板.xlsx";
				fileName= "人资管理专业技术资格导入模板.xlsx";
				break;
			case "jndj":
				filePath = "/templates/人资管理技能等级信息导入模板.xlsx";
				fileName= "人资管理技能等级信息导入模板.xlsx";
				break;
			case "dsdt":
				filePath = "/templates/人资管理导师带徒导入模板.xlsx";
				fileName= "人资管理导师带徒导入模板.xlsx";
				break;
			case "ndjx":
				filePath = "/templates/人资管理年度绩效导入模板.xlsx";
				fileName= "人资管理年度绩效导入模板.xlsx";
				break;
			case "xscg":
				filePath = "/templates/人资管理学术成果导入模板.xlsx";
				fileName= "人资管理学术成果导入模板.xlsx";
				break;
			case "zdbz":
				filePath = "/templates/人资管理制度标准导入模板.xlsx";
				fileName= "人资管理制度标准导入模板.xlsx";
				break;
			case "zlxx":
				filePath = "/templates/人资管理专利信息导入模板.xlsx";
				fileName= "人资管理专利信息导入模板.xlsx";
				break;
			case "jygx":
				filePath = "/templates/人资管理技艺革新导入模板.xlsx";
				fileName= "人资管理技艺革新导入模板.xlsx";
				break;
			case "kjcx":
				filePath = "/templates/人资管理科技创新管理导入模板.xlsx";
				fileName= "人资管理科技创新管理导入模板.xlsx";
				break;
			case "ktcg":
				filePath = "/templates/人资管理项目课题成果导入模板.xlsx";
				fileName= "人资管理项目课题成果导入模板.xlsx";
				break;
			case "ygld":
				filePath = "/templates/人资管理员工流动导入模板.xlsx";
				fileName= "人资管理员工流动导入模板.xlsx";
				break;
			case "gzlg":
				filePath = "/templates/人资管理挂职轮岗导入模板.xlsx";
				fileName= "人资管理挂职轮岗导入模板.xlsx";
				break;
			case "jxjf":
				filePath = "/templates/绩效积分信息导入模板.xlsx";
				fileName= "绩效积分信息导入模板.xlsx";
				break;
			case "fl_nc":
				filePath = "/templates/年初省公司下达计划情况表模板.xls";
				fileName= "年初省公司下达计划情况表模板.xls";
				break;
			case "fl_xyn":
				filePath = "/templates/下一年度申报计划表.xls";
				fileName= "下一年度申报计划表.xls";
				break;
			case "fl_sqtz":
				filePath = "/templates/申请调整计划情况表.xls";
				fileName= "申请调整计划情况表.xls";
				break;
			case "fl_jhts":
				filePath = "/templates/计划同申报调整相比增减情况表.xls";
				fileName= "计划同申报调整相比增减情况表.xls";
				break;
		}
		InputStream path = this.getClass().getResourceAsStream(filePath);
		// 配置文件下载
		response.setHeader("content-type", "application/octet-stream");
		response.setContentType("application/octet-stream");
		response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
		// 实现文件下载
		byte[] buffer = new byte[1024];
		BufferedInputStream bis = null;
		try {
			bis = new BufferedInputStream(path);
			OutputStream os = response.getOutputStream();
			int i = bis.read(buffer);
			while (i != -1) {
				os.write(buffer, 0, i);
				i = bis.read(buffer);
			}
			LOG.info("文件下载完成");
		} catch (Exception e) {
			LOG.error("文件下载失败！", e);
		} finally {
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}


	
}
